Cleaned up logic a bit