Major code improvements completed

Also added a library file for common functions