Cleaned up the logic