refactor of code/cleanup, squashed bugs