Lib refactored, cleaned and reviewed