comments added, reorganised some functions