Improve perf + code cleanup

@pavelgj This should be ready to merge

Replaces #19