Refactor this whole mess