updated rafael's code and killed some bugs!