Edited code according to Mikael's comments.