Optimizations + bug fixes + code clean up