Finished almost all functions, need carefully checking