Cleaned up logic