Fixed some logics on the code in order to work