added support for InputStream
added some minor code optimizations