Kind of got it working, need to refactor