some code cleanup & optimizations