Refactor and modernize code