Finished code, will be merged with master