Some extra logic
Slimmed down code quite a bit