modifs to improve speed after cProfile-ing. improve initialization