a bit of refactoring, still lots to enhance