Fixes for #4407 and #4517

Fix for:

http://dev.kohanaframework.org/issues/4407
http://dev.kohanaframework.org/issues/4157