Refactor and warnings fixes