Made the linecount function more robust, added basic registration.