this code could use some refactoring too >_>