Final version of code, only header comments added