Crazy changes, fixes bug #346

Made some changes, fixed some bugs