Minor bug fixes and optimzations.