Fixed error checking, integrated main