add memory free library
use int instead of double
refactor a little bit of code