format code and added some functions