Added a lot more of the logic