Add logs and improve code