Optimized code and added little bug fixes.