more code cleanup and refactoring its a mess