Code simplified and mostly working