Some refactoring, needs some clean to make sure things are working correctly.