Some more refactoring... some of the code is now acceptable