Added functions, corrected bugs