Fixed implementation and cleaned code