Added functions, fix errors, improve code