Cleaned up my code and abstracted it a bit.