Code optimization, bug fixes