Improve perf + code cleanup

@pavelgj This should be ready to merge