moved some code to subs and fixed some bugs