Added better implementation of sign flipping