Optimizations and Cleanups

Remove many loops and replace them with Numpy array operations. Also, fix gen_step_fun() to finally work with tied data.