cleaned up the logic