major code clean up and better module separation