Working version. Would be final version if no more bugs detected before deadline.