Cleaned up isPerfectSquare