Updated source file with several bugfixes and new features