huge cleanup + modularization