code improvements based on @dominikh comments