Did a couple of bug-fixings, and code cleaning and optimization.