Bug fixes, code cleanup, optimization.