refactored 'flags' from type int to long
unchecked fixes