code optimizations, formatting and event improvement