some cleanup and modularization