fixed the implementation based on comments by Daniel