Added comments, optimized some code, added check in preparing output binding.