added utility and modularized code

runs a bit better not according to profiling