Some bugs fixed and added some functions definitions