some refactoring, JSHint a bit happier