some refactoring to make it a bit less retarded