Almost re-finished the code