Refactored and added flip functionality