Code Cleanup

Optimization and cleanup.