Works now, would use some refactoring.