another major code clean up and refactor