restructure and fix code, begin modularization