cleaning up and refactoring some awful scripting