Improved code according to @zaggino suggestions.