moved file writing functionality to controller for properness