Slightly abstracted my code, think it needs more though