Perf Improvements and code refactaring