Add almost finished code. Possibly more refactoring to be done