cleaning code, adding better error handling, stable version
finalize